let accountFlag = false;
let nameFlag = false;
let pwFlag = false;
let repwFlag = false;
let jpgVerifyFlag = false;
let jpgVerifyFlag2 = false;
let phoneFalg = false;
let phonVerifyFlag=false;
let emailFalg = false;
let rebFalg = true;
let code="";
var counts=60;
/*function settime(val) {
    let phone = $('#phone1').val();
    if(phone==""){
        alert('请先输入手机号码！')
        return;
    }
    alert("短信验证码发送成功！")
    $.ajax({
        url:"checkMsg",
        type:"POST",
        data:{"msgPhone":phone},
        dataType:"text",
        success:function (text) {
            code=text;
        }
    })
    time(val);
}*/

function settime1(val) {
    let phone = $('#phone').val();
    if(phone==""){
        alert('请先输入手机号码！')
        return;
    }else{
        $.ajax({
            url: "/phone/code",
            type: "POST",
            data: {phoneNumber: phone},
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    alert("短信验证码发送成功！");
                } else if (text == "no") {
                    alert("短信验证码发送失败！");
                }
            }
        })

    }
    time(val);
}
function time(val){
    if(counts==0){
        val.removeAttribute("disabled");
        val.value="获取验证码";
        counts = 60 ;
        return false;
    }else{
        val.setAttribute("disabled",true);
        val.value="重新发送("+counts+")";
        counts--;
    }
    setTimeout(function () {
        time(val);
    },1000);
}




//登陆验证
$(function () {
    /*用户注册的验证和防护*/
    //账户
    $('#userAccount').blur(function () {
        let userAccount = $('#userAccount').val();
        let accountPatterm = /^[a-zA-Z]{6,10}$/
        if (!accountPatterm.test(userAccount)) {
            $('#userAccountAlt').text("✍ 账户只能是6-10个字母组成");
            $('#userAccountAlt').css("color", "red")
            $('#userAccount').val("")
            accountFlag = false;
            return;
        }
        //1.发送ajax异步请求到服务器去验证用户名在数据库是否真实存在
        $.ajax({
            url: "user/findUAccount",
            type: "GET",
            data: {registerAccount: userAccount},
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    $('#userAccountAlt').text("👌 验证通过！");
                    $('#userAccountAlt').css("color", "green")
                    accountFlag = true;

                } else if (text == "no") {
                    $('#userAccountAlt').text("该账户已存在!请重新填写");
                    $('#userAccountAlt').css("color", "red")
                    $('#userAccount').val("")
                    accountFlag = false;
                }
            }

        });
    })


    //验证密码
    let passWord1;
    let passWord2;
    $('#password').blur(function () {
        passWord1 = $('#password').val();
        let pwPatterm = /^[A-Za-z0-9]{6,16}$/
        if (pwPatterm.test(passWord1)) {
            $('#passwordAlt').text("👌 输入正确")
            $('#passwordAlt').css("color", "green")
            pwFlag=true;
        } else {
            $('#passwordAlt').text("✍ 格式不符，密码为6-16位字母数字组成")
            $('#passwordAlt').css("color", "red")
            $('#password').val("");
            pwFlag=false;

        }
    })
    $('#repeatPassword').blur(function () {
        passWord2 = $('#repeatPassword').val();
        if (passWord1 == passWord2) {
            $('#repeatPasswordAlt').text("👌 验证输入通过")
            $('#repeatPasswordAlt').css("color", "green")
            repwFlag = true;
        } else {
            $('#repeatPasswordAlt').text("✍ 二次验证未通过，请再次确认密码")
            $('#repeatPasswordAlt').css("color", "red")
            $('#repeatPassword').val("");
            repwFlag = false;
        }
    })

    //验证验证码
    $('#jpgVerify').blur(function () {
        let jpgVerify = $('#jpgVerify').val();
        if (jpgVerify==""){
            $('#jpgVerifys').text("✍ 验证码不可为空")
            $('#jpgVerifys').css("color", "red")
            $('#jpgVerify').val("");
            jpgVerifyFlag=false;
            return;
        }

        $.ajax({
            url: "code",
            type: "POST",
            data: {tryCode: jpgVerify},
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    $('#jpgVerifys').text("👌 验证通过！");
                    $('#jpgVerifys').css("color", "green")
                    jpgVerifyFlag = true;
                } else if (text == "no") {
                    $('#jpgVerifys').text("❌ 验证码错误");
                    $('#jpgVerifys').css("color", "red")
                    $('#jpgVerify').val("")
                    jpgVerifyFlag = false;
                }
            }
        })
    })

    //验证登录验证码
    $('#jpgVerify2').blur(function () {
        let jpgVerify2 = $('#jpgVerify2').val();
        if (jpgVerify2==""){
            $('#jpgVerifys2').text("✍ 验证码不可为空")
            $('#jpgVerifys2').css("color", "red")
            $('#jpgVerify2').val("");
            jpgVerifyFlag2=false;
            return;
        }

        $.ajax({
            url: "code",
            type: "POST",
            data: {tryCode: jpgVerify2},
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    $('#jpgVerifys2').text("👌 验证通过！");
                    $('#jpgVerifys2').css("color", "green")
                    jpgVerifyFlag2 = true;
                } else if (text == "no") {
                    $('#jpgVerifys2').text("❌ 验证码错误");
                    $('#jpgVerifys2').css("color", "red")
                    $('#jpgVerify2').val("")
                    jpgVerifyFlag2 = false;
                }
            }
        })
    })


    $('#phone').blur(function () {
        let phone = $('#phone').val();
        let pattern = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
        if (!pattern.test(phone)) {
            $('#phoneJy').text("❌ 输入号码格式不正确！")
            $('#phoneJy').css("color", "red")
            $('#phone').val("");
            phoneFalg = false;
            return;
        } //1.发送ajax异步请求到服务器去验证手机号码在数据库是否真实存在
        $.ajax({
            url: "user/findMobile",
            type: "GET",
            data: {userMobile: phone},
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    $('#phoneJy').text("👌 验证通过！");
                    $('#phoneJy').css("color", "green")
                    phoneFalg = true;
                } else if (text == "no") {
                    $('#phoneJy').text("❌ 手机号码已存在!请重新填写");
                    $('#phoneJy').css("color", "red")
                    $('#phone').val("")
                    phoneFalg = false;
                }
            }
        });
    })

    //验证短信验证码
    $('#phonVerify').blur(function () {
        let phonVerify = $('#phonVerify').val();
        if (phonVerify==""){
            $('#phonVerifys').text("✍ 短信验证码不可为空")
            $('#phonVerifys').css("color", "red")
            $('#phonVerify').val("");
            phonVerifyFlag=false;
            return;
        }
        $.ajax({
            url: "/phone/verifyCode",
            type: "get",
            data: {phonVerify: phonVerify},
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    $('#phonVerifys').text("👌 验证通过！");
                    $('#phonVerifys').css("color", "green")
                    phonVerifyFlag = true;
                } else if (text == "no") {
                    $('#phonVerifys').text("❌ 短信验证码错误");
                    $('#phonVerifys').css("color", "red")
                    $('#phonVerify').val("")
                    phonVerifyFlag = false;
                }
            }
        })
    })



    /*验证邮箱*/
    $('#uEmail').blur(function () {
        let email = $('#uEmail').val();
        let pattern = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        if (pattern.test(email)) {
            $('#es').text("👌 格式已通过验证")
            $('#es').css("color", "green")
            emailFalg = true;
        } else {
            $('#es').text("❌ 格式不正确，请重新检查")
            $('#es').css("color", "red")
            $('#email').val("");
            emailFalg = false;
        }
    })

    //验证姓名
    $('#uName').blur(function () {
        let userName = $('#uName').val();
        if (userName==""){
            $('#unamespan').text("✍ 用户名不可为空");
            $('#unamespan').css("color", "red");
            $('#uName').val("");
            nameFlag = false;
            return;
        }else {
            $('#unamespan').text("👌 验证输入通过");
            $('#unamespan').css("color", "green");
            nameFlag = true;
        }

    })

    //勾选协议
    $('#remember').click(function () {
        let flag = $(this).is(":checked");
        alert(flag)
        if (flag) {
            rebFalg = true;
        } else {
            rebFalg = false;
            $('#registershow').modal('show').css({"margin-top": "400px"})
        }
    })

    $('#registerBtn').click(function () {
        if (rebFalg == false) {
            $('#registershow').modal('show').css({"margin-top": "400px"})
            return;
        }
        if (!nameFlag || !pwFlag || !phoneFalg || !emailFalg || !rebFalg) {
            alert("抱歉！注册流程未通过，暂无法注册！")
            return;
        }

        $.ajax({
            url: "register",
            type: "POST",
            data: $('#checkForm').serialize(),
            dataType: "text",
            success: function (text) {
                if (text == "ok") {
                    alert("注册成功！")
                    $('#registerModal').modal('hide');
                    $('#loginModal').modal('show');
                }
            }

        })
    })





  //注册广告
    $("#advertise").hide();
    $("#advertise").delay(4000).animate({"height":"show"},3000);
    $("#closeId a").click(function () {
        $("#advertise").animate({height:"hide"},3000);
    });

})

//更换验证码
function changeyzm() {
    $('#yzm')[0].src="/defaultKaptcha?d="+new Date()*1;

}

//更换验证码
function changeyzm2() {
    $('#yzm2')[0].src="/defaultKaptcha?d="+new Date()*1;

}




function check() {
    return accountFlag&&pwFlag&&repwFlag&&jpgVerifyFlag&&phoneFalg&&phonVerifyFlag&&emailFalg&&nameFlag&&rebFalg;
}

function checkLogin() {
    let userName = $('#userName').val();
    let password = $('#password').val();
    if (userName!="" && password!="" && jpgVerifyFlag2){
        return true;
    }else {
        return false;
    }

}


